package com.chixing.service.impl;

import com.chixing.mapper.HotelImgMapper;
import com.chixing.mapper.HotelInfoMapper;
import com.chixing.mapper.OrderInfoMapper;
import com.chixing.mapper.OrdersMapper;
import com.chixing.pojo.*;
import com.chixing.service.OrderInfoService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoMapper mapper;

    @Autowired
    private OrdersMapper mapper2;
    @Autowired
    private HotelInfoMapper mapper3;
    @Autowired
    private HotelImgMapper mapper4;
    public ServerResponse getById(Integer id) {

        OrderInfo orderInfo= mapper.selectByPrimaryKey(id);
        return ServerResponse.getDataSuccess(orderInfo);
    }

    @Override
    public ServerResponse getAll() {

        List<OrderInfo> list =mapper.selectByExample(null);
        return ServerResponse.getDataSuccess(list);
    }

    @Override
    public ServerResponse delete(Integer id) {
        return null;
    }



    @Override
    public ServerResponse selectByOrderInfoId() {
        PageHelper.startPage(1,5);
        List<OrderInfo> list =mapper.selectByExample(null);
        List<OrderInfo_Order_hotelInfoVO> list5=new ArrayList<>();
        for (OrderInfo orderInfo : list) {
          OrderInfo_Order_hotelInfoVO vo=new OrderInfo_Order_hotelInfoVO();
          Integer o1 = orderInfo.getOrderId();
          Integer state=orderInfo.getOrderInfoState();
          vo.setOrderInfoState(state);//订单详情状态
            OrdersExample orders = new OrdersExample();
            orders.createCriteria().andOrderIdEqualTo(o1);
            List<Orders> list1 =mapper2.selectByExample(orders);
           /* System.out.println(list1);*/
          /*  Orders orders1 =list1.get(0);*/
            Integer hotelId =null;
            for (Orders orders2 : list1) {
                 hotelId=orders2.getHotelId();//旅馆id
                Float orderPrice =orders2.getOrderPrice();
                vo.setOrderPrice(orderPrice);//价格
                Date creatTime=orders2.getOrderTime();
                vo.setOrderTime(creatTime);//时间
                Integer state1 =orders2.getOrderState();
                vo.setOrderState(state1);//订单状态
            }
            HotelInfoExample example2=new HotelInfoExample();
            example2.createCriteria().andHotelIdEqualTo(hotelId);
            List<HotelInfo> list2=mapper3.selectByExample(example2);
           /* HotelInfo hotelInfo =list2.get(0);*/
            for (HotelInfo hotelInfo : list2) {
                /*System.out.println(hotelInfo);*/
                String name= hotelInfo.getHotelName();
                vo.setHotelName(name);
            }
            HotelImgExample example3 =new HotelImgExample();
            example3.createCriteria().andHotelIdEqualTo(hotelId);
            List<HotelImg> list3 =mapper4.selectByExample(example3);
            /*HotelImg hotelImg= list3.get(0);*/
            for (HotelImg hotelImg : list3) {
                String img=hotelImg.getImgLoc();
                vo.setImgLoc(img);
            }
            System.out.println(vo);
            list5.add(vo);
        }
        return ServerResponse.getDataSuccess(list5);

    }

    @Override
    public PageInfo<OrderInfo> getByPage(Integer pageNum) {

        PageHelper.startPage(pageNum,5);
        List<OrderInfo> list =mapper.selectByExample(null);
        PageInfo<OrderInfo> infoPageInfo =new PageInfo<>(list);

        return infoPageInfo;


    }
}
